package baseproject.com.mybaseproject.mvp.presenter

import baseproject.com.mybaseproject.net.ApiErrorModel
import baseproject.com.mybaseproject.net.ResultObserver
import baseproject.com.mybaseproject.net.RetrofitManager
import baseproject.com.mybaseproject.utils.scheduler.RxUtils
import com.bybops.app.AppConstant
import com.bybops.app.MyApplication
import com.bybops.contract.SplashContract
import com.bybops.model.api.URLConstant
import com.bybops.model.bean.AutoLogin
import com.bybops.model.bean.CheckVersion
import com.bybops.model.data.AppConfig
import com.bybops.net.BaseResponse
import com.bybops.ui.activity.MainActivity
import com.bybops.ui.activity.account.LoginActivity
import com.bybops.utils.AppUtils
import com.bybops.utils.DeviceInfoUtil
import com.bybops.utils.SPUtils

class SplashPresenterImpl : BasePresenterImpl<SplashContract.SplashView>(), SplashContract.SplashPresenter {


    /**
     * 获取配置文件
     */
    override fun getConfig() {
        addDisposable(RetrofitManager.configService.getConfig()
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<AppConfig>() {
                    override fun success(data: BaseResponse<AppConfig>) {
                        URLConstant.setURL(data.data)
                        //检测版本更新
                        checkUpdateInfo()
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        //自动登录
                        autoLogin()
                    }
                }))
    }

    /**
     * 版本检测
     */
    override fun checkUpdateInfo() {
        var params = HashMap<String, String?>()
        params["version"] = AppUtils.getVersionName(MyApplication.instance)
        params["os"] = "Android"
        addDisposable(RetrofitManager.apiService.checkUpdateInfo(params)
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<CheckVersion>() {
                    override fun success(data: BaseResponse<CheckVersion>) {
                        checkIsLatest(data.data)
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        autoLogin()
                    }
                }))
    }

    /**
     * 检测是否为最新版本
     */
    private fun checkIsLatest(checkVersion: CheckVersion) {
        if (checkVersion.ret == 1){
            //当前为最新版本
            //自动登录
            autoLogin()
        }else{
            //当前不是最新版本
            mView?.showUpdateDialog(checkVersion)
        }
    }

    /**
     * 自动登录
     */
    override fun autoLogin() {
        var params = HashMap<String, String?>()
        params["access_token"] = MyApplication.instance?.mAccessToken
        params["userKey"] = MyApplication.instance?.mUser?.userKey
        params["serial"] = DeviceInfoUtil.getDeviceId()
        addDisposable(RetrofitManager.apiService.autoLogin(params)
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<AutoLogin>() {
                    override fun success(data: BaseResponse<AutoLogin>) {
                        //判断token是否有效
                        when(data.data.tokenValid){
                            //无效  跳转到登录界面
                            -1 -> {
                                //您有30天未登录系统，请重新登录。
                                mView?.showToast(data.data.note)
                                mView?.redirectTo(LoginActivity::class.java)
                            }
                            -2 ->{
                                //您可能在其他设备上登录过，请重新登录。
                                mView?.showToast(data.data.note)
                                mView?.redirectTo(LoginActivity::class.java)
                            }
                            //有效  跳转到主界面
                            1 ->{
                                //替换新token
                                MyApplication.instance?.mAccessToken = data.data.newToken
                                SPUtils.put(AppConstant.Access_Token, data.data.newToken)
                                mView?.redirectTo(MainActivity::class.java)
                            }
                        }
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        mView?.redirectTo(LoginActivity::class.java)
                    }
                }))
    }


}